home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / texnfo-upd.el.z / texnfo-upd.el
Encoding:
Text File  |  1998-05-21  |  77.3 KB  |  2,052 lines

  1. ;;; texnfo-upd.el --- utilities for updating nodes and menus in Texinfo files
  2.  
  3. ;; Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Robert J. Chassell      
  6. ;; Maintainer: bug-texinfo@prep.ai.mit.edu
  7. ;; Keywords: maint, tex, docs
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. ;; 02111-1307, USA.
  25.  
  26. ;;; Synched up with: FSF 19.34.
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; Known bug: update commands fail to ignore @ignore.
  31.  
  32. ;; Summary: how to use the updating commands
  33.  
  34. ;; The node and menu updating functions automatically
  35.  
  36. ;;   * insert missing `@node' lines,
  37. ;;   * insert the `Next', `Previous' and `Up' pointers of a node,
  38. ;;   * insert or update the menu for a section, 
  39. ;;   * create a master menu for a Texinfo source file.
  40. ;;
  41. ;; Passed an argument, the `texinfo-update-node' and
  42. ;; `texinfo-make-menu' functions do their jobs in the region.
  43. ;;
  44. ;; In brief, the functions for creating or updating nodes and menus, are:
  45. ;; 
  46. ;;     texinfo-update-node (&optional region-p)            
  47. ;;     texinfo-every-node-update ()                        
  48. ;;     texinfo-sequential-node-update (&optional region-p)
  49. ;; 
  50. ;;     texinfo-make-menu (&optional region-p)              
  51. ;;     texinfo-all-menus-update ()                         
  52. ;;     texinfo-master-menu ()
  53. ;;
  54. ;;     texinfo-insert-node-lines  (&optional title-p)
  55. ;; 
  56. ;;     texinfo-indent-menu-description (column &optional region-p)
  57.  
  58. ;; The `texinfo-column-for-description' variable specifies the column to
  59. ;; which menu descriptions are indented. 
  60.  
  61. ;; Texinfo file structure
  62. ;; ----------------------
  63.  
  64. ;; To use the updating commands, you must structure your Texinfo file
  65. ;; hierarchically.  Each `@node' line, with the exception of the top
  66. ;; node, must be accompanied by some kind of section line, such as an
  67. ;; `@chapter' or `@section' line.  Each node-line/section-line
  68. ;; combination must look like this:
  69.  
  70. ;;      @node    Lists and Tables, Cross References, Structuring, Top
  71. ;;      @comment node-name,        next,             previous,    up
  72. ;;      @chapter Making Lists and Tables
  73.  
  74. ;; or like this (without the `@comment' line):
  75.  
  76. ;;      @node    Lists and Tables, Cross References, Structuring, Top
  77. ;;      @chapter Making Lists and Tables
  78.  
  79. ;; If the file has a `top' node, it must be called `top' or `Top' and
  80. ;; be the first node in the file.
  81.  
  82.  
  83. ;;; The update node functions described in detail
  84.  
  85. ;; The `texinfo-update-node' function without an argument inserts
  86. ;; the correct next, previous and up pointers for the node in which
  87. ;; point is located (i.e., for the node preceding point).
  88.  
  89. ;; With an argument, the `texinfo-update-node' function inserts the
  90. ;; correct next, previous and up pointers for the nodes inside the
  91. ;; region.
  92.  
  93. ;; It does not matter whether the  `@node' line has pre-existing
  94. ;; `Next', `Previous', or `Up' pointers in it.  They are removed.
  95.  
  96. ;; The `texinfo-every-node-update' function runs `texinfo-update-node'
  97. ;; on the whole buffer.
  98.  
  99. ;; The `texinfo-sequential-node-update' function inserts the
  100. ;; immediately following and preceding node into the `Next' or
  101. ;; `Previous' pointers regardless of their hierarchical level.  This is
  102. ;; only useful for certain kinds of text, like a novel, which you go
  103. ;; through sequentially.
  104.  
  105.  
  106. ;;; The menu making functions described in detail
  107.  
  108. ;; The `texinfo-make-menu' function without an argument creates or
  109. ;; updates a menu for the section encompassing the node that follows
  110. ;; point.  With an argument, it makes or updates menus for the nodes
  111. ;; within or part of the marked region.
  112.  
  113. ;; Whenever an existing menu is updated, the descriptions from
  114. ;; that menu are incorporated into the new menu.  This is done by copying
  115. ;; descriptions from the existing menu to the entries in the new menu
  116. ;; that have the same node names.  If the node names are different, the
  117. ;; descriptions are not copied to the new menu.
  118.  
  119. ;; Menu entries that refer to other Info files are removed since they
  120. ;; are not a node within current buffer.  This is a deficiency.
  121.  
  122. ;; The `texinfo-all-menus-update' function runs `texinfo-make-menu'
  123. ;; on the whole buffer.
  124.  
  125. ;; The `texinfo-master-menu' function creates an extended menu located
  126. ;; after the top node.  (The file must have a top node.)  The function
  127. ;; first updates all the regular menus in the buffer (incorporating the
  128. ;; descriptions from pre-existing menus), and then constructs a master
  129. ;; menu that includes every entry from every other menu.  (However, the
  130. ;; function cannot update an already existing master menu; if one
  131. ;; exists, it must be removed before calling the function.)
  132.  
  133. ;; The `texinfo-indent-menu-description' function indents every
  134. ;; description in the menu following point, to the specified column.
  135. ;; Non-nil argument (prefix, if interactive) means indent every
  136. ;; description in every menu in the region.  This function does not
  137. ;; indent second and subsequent lines of a multi-line description.
  138.  
  139. ;; The `texinfo-insert-node-lines' function inserts `@node' before the
  140. ;; `@chapter', `@section', and such like lines of a region in a Texinfo
  141. ;; file where the `@node' lines are missing.
  142. ;; 
  143. ;; With a non-nil argument (prefix, if interactive), the function not
  144. ;; only inserts `@node' lines but also inserts the chapter or section
  145. ;; titles as the names of the corresponding nodes; and inserts titles
  146. ;; as node names in pre-existing `@node' lines that lack names.
  147. ;; 
  148. ;; Since node names should be more concise than section or chapter
  149. ;; titles, node names so inserted will need to be edited manually.
  150.  
  151.  
  152. ;;; Code:
  153.  
  154. ;;; The menu making functions
  155.  
  156. (defun texinfo-make-menu (&optional region-p)
  157.   "Without any prefix argument, make or update a menu.
  158. Make the menu for the section enclosing the node found following point.
  159.  
  160. Non-nil argument (prefix, if interactive) means make or update menus
  161. for nodes within or part of the marked region.
  162.  
  163. Whenever a menu exists, and is being updated, the descriptions that
  164. are associated with node names in the pre-existing menu are
  165. incorporated into the new menu.  Otherwise, the nodes' section titles
  166. are inserted as descriptions."
  167.   
  168.   (interactive "P")
  169.   (if (not region-p)
  170.       (let ((level (texinfo-hierarchic-level)))
  171.         (texinfo-make-one-menu level)
  172.         (message "Done...updated the menu.  You may save the buffer."))
  173.     ;; else
  174.     (message "Making or updating menus in %s... " (buffer-name))
  175.     (let ((beginning (region-beginning))
  176.       (region-end (region-end))
  177.           (level (progn         ; find section type following point
  178.                    (goto-char (region-beginning))
  179.                    (texinfo-hierarchic-level))))
  180.       (if (= region-end beginning)
  181.           (error "Please mark a region!"))
  182.       (save-excursion
  183.         (save-restriction
  184.           (widen)
  185.           
  186.           (while  (texinfo-find-lower-level-node level region-end)
  187.             (setq level (texinfo-hierarchic-level)) ; new, lower level
  188.             (texinfo-make-one-menu level))
  189.           
  190.           (while (and (< (point) region-end)
  191.                       (texinfo-find-higher-level-node level region-end))
  192.             (setq level (texinfo-hierarchic-level))
  193.             (while (texinfo-find-lower-level-node level region-end)
  194.               (setq level (texinfo-hierarchic-level)) ; new, lower level
  195.               (texinfo-make-one-menu level))))))
  196.     (message "Done...updated menus.  You may save the buffer.")))
  197.  
  198. (defun texinfo-make-one-menu (level)
  199.   "Make a menu of all the appropriate nodes in this section.
  200. `Appropriate nodes' are those associated with sections that are 
  201. at the level specified by LEVEL.  Point is left at the end of menu."
  202.   (let*
  203.       ((case-fold-search t)
  204.        (beginning
  205.     (save-excursion
  206.       (goto-char (texinfo-update-menu-region-beginning level))
  207.       (end-of-line)
  208.       (point)))
  209.        (end (texinfo-update-menu-region-end level))
  210.        (first (texinfo-menu-first-node beginning end))
  211.        (node-name (progn
  212.                     (goto-char beginning)
  213.                     (beginning-of-line)
  214.                     (texinfo-copy-node-name)))
  215.        (new-menu-list (texinfo-make-menu-list beginning end level)))
  216.     (if (texinfo-old-menu-p beginning first)
  217.         (progn
  218.           (texinfo-incorporate-descriptions new-menu-list)
  219.           (texinfo-incorporate-menu-entry-names new-menu-list)
  220.           (texinfo-delete-old-menu beginning first)))
  221.     (texinfo-insert-menu new-menu-list node-name)))
  222.  
  223. (defun texinfo-all-menus-update (&optional update-all-nodes-p)
  224.   "Update every regular menu in a Texinfo file.
  225. Update pre-existing master menu, if there is one.
  226.  
  227. If called with a non-nil argument, this function first updates all the
  228. nodes in the buffer before updating the menus."
  229.   (interactive "P")
  230.   (let ((case-fold-search t)
  231.         master-menu-p)
  232.     (save-excursion
  233.       (push-mark (point-max) t)
  234.       (goto-char (point-min))
  235.       (message "Checking for a master menu in %s ... "(buffer-name))
  236.       (save-excursion
  237.         (if (re-search-forward texinfo-master-menu-header nil t)
  238.             ;; Remove detailed master menu listing
  239.             (progn
  240.               (setq master-menu-p t)
  241.               (goto-char (match-beginning 0))
  242.               (let ((end-of-detailed-menu-descriptions
  243.                      (save-excursion     ; beginning of end menu line
  244.                        (goto-char (texinfo-menu-end))
  245.                        (beginning-of-line) (forward-char -1)
  246.                        (point))))
  247.                 (delete-region (point) end-of-detailed-menu-descriptions)))))
  248.       
  249.       (if update-all-nodes-p
  250.           (progn
  251.             (message "Updating all nodes in %s ... " (buffer-name))
  252.             (sleep-for 2)
  253.             (push-mark (point-max) t)
  254.             (goto-char (point-min))
  255.         ;; Using the mark to pass bounds this way
  256.         ;; is kludgy, but it's not worth fixing. -- rms.
  257.         (let ((mark-active t))
  258.           (texinfo-update-node t))))
  259.       
  260.       (message "Updating all menus in %s ... " (buffer-name))        
  261.       (sleep-for 2)
  262.       (push-mark (point-max) t)
  263.       (goto-char (point-min))
  264.       ;; Using the mark to pass bounds this way
  265.       ;; is kludgy, but it's not worth fixing. -- rms.
  266.       (let ((mark-active t))
  267.     (texinfo-make-menu t))
  268.       
  269.       (if master-menu-p
  270.           (progn
  271.             (message "Updating the master menu in %s... " (buffer-name))
  272.             (sleep-for 2)
  273.             (texinfo-master-menu nil))))
  274.     
  275.     (message "Done...updated all the menus.  You may save the buffer.")))
  276.  
  277. (defun texinfo-find-lower-level-node (level region-end)
  278.   "Search forward from point for node at any level lower than LEVEL.
  279. Search is limited to the end of the marked region, REGION-END, 
  280. and to the end of the menu region for the level.
  281.  
  282. Return t if the node is found, else nil.  Leave point at the beginning
  283. of the node if one is found; else do not move point."
  284.   (let ((case-fold-search t))
  285.     (if (and (< (point) region-end)
  286.              (re-search-forward
  287.               (concat
  288.                "\\(^@node\\).*\n"         ; match node line
  289.                "\\(\\(\\(^@c\\).*\n\\)"   ; match comment line, if any
  290.                "\\|"                      ; or
  291.                "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
  292.                (eval (cdr (assoc level texinfo-update-menu-lower-regexps))))
  293.               ;; the next higher level node marks the end of this
  294.               ;; section, and no lower level node will be found beyond
  295.               ;; this position even if region-end is farther off
  296.               (texinfo-update-menu-region-end level) 
  297.               t))
  298.         (goto-char (match-beginning 1)))))
  299.  
  300. (defun texinfo-find-higher-level-node (level region-end)
  301.   "Search forward from point for node at any higher level than argument LEVEL.
  302. Search is limited to the end of the marked region, REGION-END.
  303.  
  304. Return t if the node is found, else nil.  Leave point at the beginning
  305. of the node if one is found; else do not move point."
  306.   (let ((case-fold-search t))
  307.     (cond
  308.      ((or (string-equal "top" level) (string-equal "chapter" level))
  309.       (if (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" region-end t)
  310.           (progn (beginning-of-line) t)))
  311.      (t
  312.       (if (re-search-forward
  313.            (concat
  314.             "\\(^@node\\).*\n"              ; match node line
  315.             "\\(\\(\\(^@c\\).*\n\\)"        ; match comment line, if any
  316.             "\\|"                           ; or
  317.             "\\(^@ifinfo[ ]*\n\\)\\)?"      ; ifinfo line, if any
  318.             (eval (cdr (assoc level texinfo-update-menu-higher-regexps))))
  319.            region-end t)
  320.           (progn (beginning-of-line) t))))))
  321.  
  322.  
  323. ;;; Making the list of new menu entries
  324.  
  325. (defun texinfo-make-menu-list (beginning end level)
  326.   "Make a list of node names and their descriptions.
  327. Point is left at the end of the menu region, but the menu is not inserted.
  328.  
  329. First argument is position from which to start making menu list; 
  330. second argument is end of region in which to try to locate entries;
  331. third argument is the level of the nodes that are the entries.
  332.  
  333. Node names and descriptions are dotted pairs of strings.  Each pair is
  334. an element of the list.  If the description does not exist, the
  335. element consists only of the node name."
  336.   (goto-char beginning)
  337.   (let (new-menu-list)
  338.     (while (texinfo-menu-locate-entry-p level end)
  339.       (setq new-menu-list 
  340.             (cons (cons 
  341.                    (texinfo-copy-node-name)
  342.                    (prog1 "" (forward-line 1)))
  343.                    ;; Use following to insert section titles automatically.
  344.                    ;; (texinfo-copy-section-title))
  345.                   new-menu-list)))
  346.     (reverse new-menu-list)))
  347.  
  348. (defun texinfo-menu-locate-entry-p (level search-end)
  349.   "Find a node that will be part of menu for this section.
  350. First argument is a string such as \"section\" specifying the general
  351. hierarchical level of the menu; second argument is a position
  352. specifying the end of the search.
  353.  
  354. The function returns t if the node is found, else nil.  It searches
  355. forward from point, and leaves point at the beginning of the node.
  356.  
  357. The function finds entries of the same type.  Thus `subsections' and
  358. `unnumberedsubsecs' will appear in the same menu."
  359.   (let ((case-fold-search t))
  360.     (if (re-search-forward
  361.          (concat
  362.           "\\(^@node\\).*\n"              ; match node line
  363.           "\\(\\(\\(^@c\\).*\n\\)"        ; match comment line, if any
  364.           "\\|"                           ; or
  365.           "\\(^@ifinfo[ ]*\n\\)\\)?"      ; ifinfo line, if any
  366.           (eval
  367.            (cdr (assoc level texinfo-update-menu-same-level-regexps))))
  368.          search-end
  369.          t)
  370.         (goto-char (match-beginning 1)))))
  371.  
  372. (defun texinfo-copy-node-name ()
  373.   "Return the node name as a string.
  374.  
  375. Start with point at the beginning of the node line; copy the text
  376. after the node command up to the first comma on the line, if any, and
  377. return the text as a string.  Leaves point at the beginning of the
  378. line.  If there is no node name, returns an empty string."
  379.   
  380.   (save-excursion
  381.     (buffer-substring
  382.      (progn (forward-word 1)              ; skip over node command
  383.             (skip-chars-forward " \t")    ; and over spaces
  384.             (point))
  385.      (if (search-forward
  386.           ","
  387.           (save-excursion (end-of-line) (point)) t) ; bound search
  388.          (1- (point))
  389.        (end-of-line) (point)))))
  390.  
  391. (defun texinfo-copy-section-title ()
  392.   "Return the title of the section as a string.
  393. The title is used as a description line in the menu when one does not
  394. already exist.
  395.  
  396. Move point to the beginning of the appropriate section line by going
  397. to the start of the text matched by last regexp searched for, which
  398. must have been done by `texinfo-menu-locate-entry-p'."
  399.  
  400.   ;; could use the same re-search as in `texinfo-menu-locate-entry-p'
  401.   ;; instead of using `match-beginning'; such a variation would be
  402.   ;; more general, but would waste information already collected
  403.  
  404.   (goto-char (match-beginning 7))       ; match section name 
  405.  
  406.   (buffer-substring
  407.    (progn (forward-word 1)              ; skip over section type
  408.           (skip-chars-forward " \t")    ; and over spaces
  409.           (point))
  410.    (progn (end-of-line) (point))))
  411.  
  412.  
  413. ;;; Handling the old menu
  414.  
  415. (defun texinfo-old-menu-p (beginning first)
  416.   "Move point to the beginning of the menu for this section, if any.
  417. Otherwise move point to the end of the first node of this section.
  418. Return t if a menu is found, nil otherwise.
  419.  
  420. First argument is the position of the beginning of the section in which
  421. the menu will be located; second argument is the position of the first
  422. node within the section.
  423.  
  424. If no menu is found, the function inserts two newlines just before the
  425. end of the section, and leaves point there where a menu ought to be."
  426.   (goto-char beginning)
  427.   (if (not (re-search-forward "^@menu" first 'goto-end))
  428.       (progn (insert "\n\n") (forward-line -2) nil)
  429.     t))
  430.  
  431. (defun texinfo-incorporate-descriptions (new-menu-list)
  432.   "Copy the old menu line descriptions that exist to the new menu.
  433.  
  434. Point must be at beginning of old menu.
  435.  
  436. If the node-name of the new menu is found in the old menu, insert the
  437. old description into the new entry.
  438.  
  439. For this function, the new menu is a list made up of lists of dotted
  440. pairs in which the first element of the pair is the node name and the
  441. second element the description.  The new menu is changed destructively.
  442. The old menu is the menu as it appears in the texinfo file."
  443.   
  444.   (let ((new-menu-list-pointer new-menu-list)
  445.         (end-of-menu (texinfo-menu-end)))
  446.     (while new-menu-list
  447.       (save-excursion                   ; keep point at beginning of menu 
  448.         (if (re-search-forward
  449.              ;; Existing nodes can have the form
  450.              ;;     * NODE NAME:: DESCRIPTION
  451.              ;; or
  452.              ;;     * MENU ITEM: NODE NAME.     DESCRIPTION.
  453.              ;; 
  454.              ;; Recognize both when looking for the description.
  455.              (concat "\\* \\("              ; so only menu entries are found
  456.                      (car (car new-menu-list)) "::"
  457.                      "\\|"
  458.                      ".*: " (car (car new-menu-list)) "[.,\t\n]"
  459.                      "\\)"
  460.                      )               ; so only complete entries are found
  461.              end-of-menu
  462.              t) 
  463.             (setcdr (car new-menu-list) 
  464.                     (texinfo-menu-copy-old-description end-of-menu))))
  465.       (setq new-menu-list (cdr new-menu-list))) 
  466.     (setq new-menu-list new-menu-list-pointer)))
  467.  
  468. (defun texinfo-incorporate-menu-entry-names (new-menu-list)
  469.   "Copy any old menu entry names to the new menu.
  470.  
  471. Point must be at beginning of old menu.
  472.  
  473. If the node-name of the new menu entry cannot be found in the old
  474. menu, do nothing.
  475.  
  476. For this function, the new menu is a list made up of lists of dotted
  477. pairs in which the first element of the pair is the node name and the
  478. second element is the description (or nil).
  479.  
  480. If we find an existing menu entry name, we change the first element of
  481. the pair to be another dotted pair in which the car is the menu entry
  482. name and the cdr is the node name.
  483.  
  484. NEW-MENU-LIST is changed destructively.  The old menu is the menu as it
  485. appears in the texinfo file."
  486.   
  487.   (let ((new-menu-list-pointer new-menu-list)
  488.         (end-of-menu (texinfo-menu-end)))
  489.     (while new-menu-list
  490.       (save-excursion                   ; keep point at beginning of menu 
  491.         (if (re-search-forward
  492.              ;; Existing nodes can have the form
  493.              ;;     * NODE NAME:: DESCRIPTION
  494.              ;; or
  495.              ;;     * MENU ITEM: NODE NAME.     DESCRIPTION.
  496.              ;; 
  497.              ;; We're interested in the second case.
  498.              (concat "\\* "              ; so only menu entries are found
  499.                      "\\(.*\\): " (car (car new-menu-list))  "[.,\t\n]")
  500.              end-of-menu
  501.              t)
  502.             (setcar
  503.               (car new-menu-list)  ; replace the node name
  504.               (cons (buffer-substring (match-beginning 1) (match-end 1))
  505.                     (car (car new-menu-list)))))
  506.       (setq new-menu-list (cdr new-menu-list))))
  507.     (setq new-menu-list new-menu-list-pointer)))
  508.  
  509. (defun texinfo-menu-copy-old-description (end-of-menu)
  510.   "Return description field of old menu line as string.
  511. Point must be located just after the node name.  Point left before description.
  512. Single argument, END-OF-MENU, is position limiting search."
  513.   (skip-chars-forward "[:.,\t\n ]+")
  514.   ;; don't copy a carriage return at line beginning with asterisk!
  515.   ;; do copy a description that begins with an `@'!
  516.   ;; !! Known bug: does not copy descriptions starting with ^|\{?* etc.
  517.   (if (and (looking-at "\\(\\w+\\|@\\)")    
  518.            (not (looking-at "\\(^\\* \\|^@end menu\\)")))  
  519.       (buffer-substring
  520.        (point)
  521.        (save-excursion
  522.          (re-search-forward "\\(^\\* \\|^@end menu\\)" end-of-menu t)
  523.          (forward-line -1)
  524.          (end-of-line)                  ; go to end of last description line
  525.          (point)))
  526.     ""))
  527.  
  528. (defun texinfo-menu-end ()
  529.   "Return position of end of menu. Does not change location of point.
  530. Signal an error if not end of menu."
  531.   (save-excursion
  532.     (if (re-search-forward "^@end menu" nil t)
  533.         (point)
  534.       (error "Menu does not have an end."))))
  535.  
  536. (defun texinfo-delete-old-menu (beginning first)
  537.   "Delete the old menu.  Point must be in or after menu.
  538. First argument is position of the beginning of the section in which
  539. the menu will be located; second argument is the position of the first
  540. node within the section."
  541.   ;; No third arg to search, so error if search fails.
  542.   (re-search-backward "^@menu" beginning)
  543.   (delete-region (point)
  544.                  (save-excursion
  545.                    (re-search-forward "^@end menu" first)
  546.                    (point))))
  547.  
  548.  
  549. ;;; Inserting new menu
  550.  
  551. ;; try 32, but perhaps 24 is better
  552. (defvar texinfo-column-for-description 32
  553.   "*Column at which descriptions start in a Texinfo menu.")
  554.  
  555. (defun texinfo-insert-menu (menu-list node-name)
  556.   "Insert formatted menu at point.
  557. Indents the first line of the description, if any, to the value of
  558. texinfo-column-for-description.
  559.  
  560. MENU-LIST has form:
  561.  
  562.     \(\(\"node-name1\" . \"description\"\) 
  563.     \(\"node-name2\" . \"description\"\) ... \)
  564.  
  565. However, the description field might be nil.
  566.  
  567. Also, the node-name field might itself be a dotted pair (call it P) of
  568. strings instead of just a string.  In that case, the car of P
  569. is the menu entry name, and the cdr of P is the node name."
  570.   
  571.   (insert "@menu\n")
  572.   (while menu-list
  573.     ;; Every menu entry starts with a star and a space.
  574.     (insert "* ")
  575.     
  576.     ;; Insert the node name (and menu entry name, if present).
  577.     (let ((node-part (car (car menu-list))))
  578.       (if (stringp node-part)
  579.           ;; "Double colon" entry line; menu entry and node name are the same,
  580.           (insert (format "%s::" node-part))  
  581.         ;; "Single colon" entry line; menu entry and node name are different.
  582.         (insert (format "%s: %s." (car node-part) (cdr node-part)))))
  583.     
  584.     ;; Insert the description, if present.
  585.     (if (cdr (car menu-list))
  586.         (progn
  587.           ;; Move to right place.
  588.           (indent-to texinfo-column-for-description 2) 
  589.           ;; Insert description.
  590.           (insert (format "%s" (cdr (car menu-list))))))  
  591.  
  592.     (insert "\n") ; end this menu entry
  593.     (setq menu-list (cdr menu-list)))
  594.   (insert "@end menu")
  595.   (message 
  596.    "Updated \"%s\" level menu following node: %s ... " level node-name))
  597.  
  598.  
  599. ;;; Starting menu descriptions by inserting titles
  600.  
  601. (defun texinfo-start-menu-description ()
  602.   "In this menu entry, insert the node's section title as a description. 
  603. Position point at beginning of description ready for editing.
  604. Do not insert a title if the line contains an existing description.
  605.  
  606. You will need to edit the inserted text since a useful description
  607. complements the node name rather than repeats it as a title does."
  608.   
  609.   (interactive)
  610.   (let (beginning end node-name title)
  611.     (save-excursion
  612.     (beginning-of-line)  
  613.       (if (search-forward "* " (save-excursion (end-of-line) (point)) t)
  614.           (progn (skip-chars-forward " \t")
  615.                  (setq beginning (point)))
  616.         (error "This is not a line in a menu!"))
  617.       
  618.       (cond
  619.         ;; "Double colon" entry line; menu entry and node name are the same,
  620.        ((search-forward "::" (save-excursion (end-of-line) (point)) t)
  621.         (if (looking-at "[ \t]*[^ \t\n]+")
  622.             (error "Descriptive text already exists."))
  623.         (skip-chars-backward ": \t")
  624.         (setq node-name (buffer-substring beginning (point))))
  625.        
  626.        ;; "Single colon" entry line; menu entry and node name are different.
  627.        ((search-forward ":" (save-excursion (end-of-line) (point)) t)
  628.         (skip-chars-forward " \t")
  629.         (setq beginning (point))
  630.         ;; Menu entry line ends in a period, comma, or tab. 
  631.         (if (re-search-forward "[.,\t]"
  632.                                (save-excursion (forward-line 1) (point)) t)
  633.             (progn
  634.               (if (looking-at "[ \t]*[^ \t\n]+")
  635.                   (error "Descriptive text already exists."))
  636.               (skip-chars-backward "., \t")
  637.               (setq node-name (buffer-substring beginning (point))))
  638.           ;; Menu entry line ends in a return.
  639.           (re-search-forward ".*\n"
  640.                            (save-excursion (forward-line 1) (point)) t)
  641.           (skip-chars-backward " \t\n")
  642.           (setq node-name (buffer-substring beginning (point)))
  643.           (if (= 0 (length node-name))
  644.               (error "No node name on this line.")
  645.             (insert "."))))
  646.        (t (error "No node name on this line.")))
  647.       ;; Search for node that matches node name, and copy the section title.
  648.       (if (re-search-forward
  649.            (concat 
  650.             "^@node[ \t]+"
  651.             node-name
  652.             ".*\n"                             ; match node line
  653.             "\\("
  654.             "\\(\\(^@c \\|^@comment\\).*\n\\)" ; match comment line, if any
  655.             "\\|"                              ; or
  656.             "\\(^@ifinfo[ ]*\n\\)"             ; ifinfo line, if any
  657.             "\\)?")
  658.            nil t)
  659.           (progn
  660.             (setq title
  661.                   (buffer-substring
  662.                    ;; skip over section type
  663.                    (progn (forward-word 1) 
  664.                           ;; and over spaces
  665.                           (skip-chars-forward " \t") 
  666.                           (point))
  667.                    (progn (end-of-line)
  668.                           (skip-chars-backward " \t")
  669.                           (point)))))
  670.         (error "Cannot find node to match node name in menu entry.")))
  671.     ;; Return point to the menu and insert the title.
  672.     (end-of-line)
  673.     (delete-region
  674.      (point)
  675.      (save-excursion (skip-chars-backward " \t") (point)))
  676.     (indent-to texinfo-column-for-description 2)
  677.     (save-excursion (insert title))))
  678.  
  679.  
  680. ;;; Handling description indentation
  681.  
  682. ; Since the make-menu functions indent descriptions, these functions
  683. ; are useful primarily for indenting a single menu specially.
  684.  
  685. (defun texinfo-indent-menu-description (column &optional region-p)
  686.   "Indent every description in menu following point to COLUMN.  
  687. Non-nil argument (prefix, if interactive) means indent every
  688. description in every menu in the region.  Does not indent second and
  689. subsequent lines of a multi-line description."
  690.   
  691.   (interactive
  692.    "nIndent menu descriptions to (column number): \nP")
  693.   (save-excursion
  694.     (save-restriction
  695.       (widen)
  696.       (if (not region-p)
  697.           (progn
  698.             (re-search-forward "^@menu")
  699.             (texinfo-menu-indent-description column)
  700.             (message
  701.              "Indented descriptions in menu.  You may save the buffer."))
  702.         ;;else
  703.         (message "Indenting every menu description in region... ")
  704.         (goto-char (region-beginning))
  705.         (while (and (< (point) (region-end))
  706.                     (texinfo-locate-menu-p))
  707.           (forward-line 1)
  708.           (texinfo-menu-indent-description column))
  709.         (message "Indenting done.  You may save the buffer.")))))
  710.  
  711. (defun texinfo-menu-indent-description (to-column-number)
  712.   "Indent the Texinfo file menu description to TO-COLUMN-NUMBER.
  713. Start with point just after the word `menu' in the `@menu' line and
  714. leave point on the line before the `@end menu' line.  Does not indent
  715. second and subsequent lines of a multi-line description."
  716.   (let* ((beginning-of-next-line (point)))
  717.     (while (< beginning-of-next-line
  718.               (save-excursion     ; beginning of end menu line
  719.                 (goto-char (texinfo-menu-end))
  720.                 (beginning-of-line)
  721.                 (point)))
  722.  
  723.       (if (re-search-forward  "\\* \\(.*::\\|.*: [^.,\t\n]+[.,\t]\\)" 
  724.            (texinfo-menu-end) 
  725.            t)
  726.           (progn
  727.             (let ((beginning-white-space (point)))
  728.               (skip-chars-forward " \t")  ; skip over spaces
  729.               (if (looking-at "\\(@\\|\\w\\)+") ; if there is text
  730.                   (progn
  731.                     ;; remove pre-existing indentation
  732.                     (delete-region beginning-white-space (point))
  733.                     (indent-to-column to-column-number))))))
  734.       ;; position point at beginning of next line
  735.       (forward-line 1)                  
  736.       (setq beginning-of-next-line (point)))))
  737.  
  738.  
  739. ;;; Making the master menu
  740.  
  741. (defun texinfo-master-menu (update-all-nodes-menus-p)
  742.   "Make a master menu for a whole Texinfo file.
  743. Non-nil argument (prefix, if interactive) means first update all
  744. existing nodes and menus.  Remove pre-existing master menu, if there is one.
  745.  
  746. This function creates a master menu that follows the top node.  The
  747. master menu includes every entry from all the other menus.  It
  748. replaces any existing ordinary menu that follows the top node.
  749.  
  750. If called with a non-nil argument, this function first updates all the
  751. menus in the buffer (incorporating descriptions from pre-existing
  752. menus) before it constructs the master menu.
  753.  
  754. The function removes the detailed part of an already existing master
  755. menu.  This action depends on the pre-existing master menu using the
  756. standard `texinfo-master-menu-header'.
  757.  
  758. The master menu has the following format, which is adapted from the
  759. recommendation in the Texinfo Manual:
  760.  
  761.    * The first part contains the major nodes in the Texinfo file: the
  762.      nodes for the chapters, chapter-like sections, and the major
  763.      appendices.  This includes the indices, so long as they are in
  764.      chapter-like sections, such as unnumbered sections.
  765.  
  766.    * The second and subsequent parts contain a listing of the other,
  767.      lower level menus, in order.  This way, an inquirer can go
  768.      directly to a particular node if he or she is searching for
  769.      specific information.
  770.  
  771. Each of the menus in the detailed node listing is introduced by the
  772. title of the section containing the menu."
  773.   
  774.   (interactive "P")
  775.   (let ((case-fold-search t))
  776.     (widen)
  777.     (goto-char (point-min))
  778.     
  779.     ;; Move point to location after `top'.
  780.     (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
  781.         (error "This buffer needs a Top node!"))
  782.     
  783.     (let ((first-chapter                  
  784.            (save-excursion
  785.              (or (re-search-forward "^@node" nil t)
  786.                  (error "Too few nodes for a master menu!"))
  787.              (point))))
  788.       (if (re-search-forward texinfo-master-menu-header first-chapter t)
  789.           ;; Remove detailed master menu listing
  790.           (progn
  791.             (goto-char (match-beginning 0))
  792.             (let ((end-of-detailed-menu-descriptions
  793.                    (save-excursion     ; beginning of end menu line
  794.                      (goto-char (texinfo-menu-end))
  795.                      (beginning-of-line) (forward-char -1)
  796.                      (point))))
  797.               (delete-region (point) end-of-detailed-menu-descriptions)))))
  798.     
  799.     (if update-all-nodes-menus-p
  800.         (progn
  801.           (message "Making a master menu in %s ...first updating all nodes... "
  802.                    (buffer-name))
  803.           (sleep-for 2)
  804.           (push-mark (point-max) t)
  805.           (goto-char (point-min))
  806.           (texinfo-update-node t)
  807.           
  808.           (message "Updating all menus in %s ... " (buffer-name))        
  809.           (sleep-for 2)
  810.           (push-mark (point-max) t)
  811.           (goto-char (point-min))
  812.           (texinfo-make-menu t)))
  813.     
  814.     (message "Now making the master menu in %s... " (buffer-name))
  815.     (sleep-for 2)
  816.     (goto-char (point-min))
  817.     (texinfo-insert-master-menu-list
  818.      (texinfo-master-menu-list))
  819.     
  820.     ;; Remove extra newlines that texinfo-insert-master-menu-list
  821.     ;; may have inserted.
  822.     
  823.     (save-excursion
  824.       (goto-char (point-min))
  825.       
  826.       (if (re-search-forward texinfo-master-menu-header nil t)
  827.           (progn
  828.             (goto-char (match-beginning 0))
  829.             (insert "\n")
  830.             (delete-blank-lines)
  831.             (goto-char (point-min))))
  832.  
  833.       (re-search-forward "^@menu")
  834.       (forward-line -1)
  835.       (delete-blank-lines)
  836.       
  837.       (re-search-forward "^@end menu")
  838.       (forward-line 1)
  839.       (delete-blank-lines))
  840.     
  841.     (message
  842.      "Done...completed making master menu.  You may save the buffer.")))
  843.  
  844. (defun texinfo-master-menu-list ()
  845.   "Return a list of menu entries and header lines for the master menu.
  846.  
  847. Start with the menu for chapters and indices and then find each
  848. following menu and the title of the node preceding that menu.
  849.  
  850. The master menu list has this form:
  851.  
  852.     \(\(\(... \"entry-1-2\"  \"entry-1\"\) \"title-1\"\)
  853.       \(\(... \"entry-2-2\"  \"entry-2-1\"\) \"title-2\"\)
  854.       ...\)
  855.  
  856. However, there does not need to be a title field."
  857.  
  858.   (let (master-menu-list)
  859.     (while (texinfo-locate-menu-p)
  860.       (setq master-menu-list 
  861.             (cons (list
  862.                    (texinfo-copy-menu)
  863.                    (texinfo-copy-menu-title))
  864.                   master-menu-list)))
  865.     (reverse master-menu-list)))
  866.  
  867. (defun texinfo-insert-master-menu-list (master-menu-list)
  868.   "Format and insert the master menu in the current buffer."
  869.   (goto-char (point-min))
  870.   ;; Insert a master menu only after `Top' node and before next node
  871.   ;; \(or include file if there is no next node\).
  872.   (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
  873.       (error "This buffer needs a Top node!"))
  874.   (let ((first-chapter
  875.          (save-excursion (re-search-forward "^@node\\|^@include") (point))))
  876.     (if (not (re-search-forward "^@menu" first-chapter t))
  877.         (error
  878.          "Buffer lacks ordinary `Top' menu in which to insert master.")))
  879.   (beginning-of-line)
  880.   (delete-region      ; buffer must have ordinary top menu
  881.    (point)   
  882.    (save-excursion (re-search-forward "^@end menu") (point)))
  883.   
  884.   (save-excursion                       ; leave point at beginning of menu
  885.     ;; Handle top of menu
  886.     (insert "\n@menu\n")
  887.     ;; Insert chapter menu entries
  888.     (setq this-very-menu-list (reverse (car (car master-menu-list))))
  889.     ;; Tell user what is going on.
  890.     (message "Inserting chapter menu entry: %s ... " this-very-menu-list)
  891.     (while this-very-menu-list
  892.       (insert "* " (car this-very-menu-list) "\n")
  893.       (setq this-very-menu-list (cdr this-very-menu-list)))
  894.     
  895.     (setq master-menu-list (cdr master-menu-list))
  896.     
  897.     ;; Only insert detailed master menu if there is one....
  898.     (if (car (car master-menu-list))
  899.         (insert texinfo-master-menu-header))
  900.     
  901.     ;; Now, insert all the other menus
  902.     
  903.     ;; The menu master-menu-list has a form like this:
  904.     ;; ((("beta"  "alpha") "title-A")
  905.     ;;  (("delta" "gamma") "title-B"))
  906.     
  907.     (while master-menu-list
  908.       
  909.       (message
  910.        "Inserting menu for %s .... " (car (cdr (car master-menu-list))))
  911.       ;; insert title of menu section
  912.       (insert "\n" (car (cdr (car master-menu-list))) "\n\n")
  913.       
  914.       ;; insert each menu entry
  915.       (setq this-very-menu-list (reverse (car (car master-menu-list))))
  916.       (while this-very-menu-list
  917.         (insert "* " (car this-very-menu-list) "\n")
  918.         (setq this-very-menu-list (cdr this-very-menu-list)))
  919.       
  920.       (setq master-menu-list (cdr master-menu-list)))
  921.     
  922.     ;; Finish menu
  923.     (insert "@end menu\n\n")))
  924.  
  925. (defvar texinfo-master-menu-header
  926.   "\n --- The Detailed Node Listing ---\n"
  927.   "String inserted before lower level entries in Texinfo master menu.
  928. It comes after the chapter-level menu entries.")
  929.  
  930. (defun texinfo-locate-menu-p ()
  931.   "Find the next menu in the texinfo file.
  932. If found, leave point after word `menu' on the `@menu' line, and return t.
  933. If a menu is not found, do not move point and return nil."
  934.   (re-search-forward "\\(^@menu\\)" nil t))
  935.  
  936. (defun texinfo-copy-menu-title  ()
  937.   "Return the title of the section preceding the menu as a string.
  938. If such a title cannot be found, return an empty string.  Do not move
  939. point."
  940.   (let ((case-fold-search t))
  941.     (save-excursion
  942.       (if (re-search-backward
  943.            (concat
  944.             "\\(^@top"
  945.             "\\|"                         ; or
  946.             texinfo-section-types-regexp  ; all other section types
  947.             "\\)")
  948.            nil
  949.            t)
  950.           (progn
  951.             (beginning-of-line)
  952.             (forward-word 1)              ; skip over section type
  953.             (skip-chars-forward " \t")    ; and over spaces
  954.             (buffer-substring
  955.              (point)
  956.              (progn (end-of-line) (point))))
  957.         ""))))
  958.  
  959. (defun texinfo-copy-menu ()
  960.   "Return the entries of an existing menu as a list.
  961. Start with point just after the word `menu' in the `@menu' line
  962. and leave point on the line before the `@end menu' line."
  963.   (let* (this-menu-list
  964.          (end-of-menu (texinfo-menu-end)) ; position of end of `@end menu'
  965.          (last-entry (save-excursion      ; position of beginning of
  966.                                           ; last `* ' entry
  967.                       (goto-char end-of-menu)
  968.                       ;; handle multi-line description
  969.                       (if (not (re-search-backward "^\\* " nil t))
  970.                           (error "No entries in menu."))
  971.                       (point))))
  972.     (while (< (point) last-entry)
  973.       (if (re-search-forward  "^\\* " end-of-menu t)
  974.           (progn
  975.             (setq this-menu-list
  976.                   (cons
  977.                    (buffer-substring 
  978.                     (point)
  979.                     ;; copy multi-line descriptions
  980.                     (save-excursion
  981.                       (re-search-forward "\\(^\\* \\|^@e\\)" nil t)
  982.                       (- (point) 3)))
  983.                    this-menu-list)))))
  984.     this-menu-list))
  985.  
  986.  
  987. ;;; Determining the hierarchical level in the texinfo file
  988.  
  989. (defun texinfo-specific-section-type () 
  990.   "Return the specific type of next section, as a string.
  991. For example, \"unnumberedsubsec\".  Return \"top\" for top node.
  992.  
  993. Searches forward for a section.  Hence, point must be before the
  994. section whose type will be found.  Does not move point.  Signal an
  995. error if the node is not the top node and a section is not found."
  996.   (let ((case-fold-search t))
  997.     (save-excursion
  998.       (cond
  999.        ((re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
  1000. ;;; Following search limit by cph but causes a bug
  1001. ;;;             (save-excursion
  1002. ;;;               (end-of-line)
  1003. ;;;               (point))
  1004.                            nil
  1005.                            t)
  1006.         "top")
  1007.        ((re-search-forward texinfo-section-types-regexp nil t)
  1008.     (buffer-substring-no-properties
  1009.      (progn (beginning-of-line) ; copy its name
  1010.         (1+ (point)))
  1011.      (progn (forward-word 1)
  1012.         (point))))
  1013.        (t
  1014.         (error
  1015.          "texinfo-specific-section-type: Chapter or section not found."))))))
  1016.  
  1017. (defun texinfo-hierarchic-level ()
  1018.   "Return the general hierarchal level of the next node in a texinfo file.
  1019. Thus, a subheading or appendixsubsec is of type subsection."
  1020.   (let ((case-fold-search t))
  1021.     (cdr (assoc
  1022.           (texinfo-specific-section-type)
  1023.           texinfo-section-to-generic-alist))))
  1024.  
  1025.  
  1026. ;;; Locating the major positions
  1027.  
  1028. (defun texinfo-update-menu-region-beginning (level)  
  1029.   "Locate beginning of higher level section this section is within.
  1030. Return position of the beginning of the node line; do not move point.
  1031. Thus, if this level is subsection, searches backwards for section node.
  1032. Only argument is a string of the general type of section."
  1033.   (let ((case-fold-search t))
  1034.     ;; !! Known bug: if section immediately follows top node, this
  1035.     ;; returns the beginning of the buffer as the beginning of the
  1036.     ;; higher level section.
  1037.     (cond
  1038.      ((or (string-equal "top" level)
  1039.           (string-equal "chapter" level))
  1040.       (save-excursion
  1041.         (goto-char (point-min))
  1042.         (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t)
  1043.         (beginning-of-line)
  1044.         (point)))
  1045.      (t
  1046.       (save-excursion
  1047.         (re-search-backward
  1048.          (concat
  1049.           "\\(^@node\\).*\n"              ; match node line
  1050.           "\\(\\(\\(^@c\\).*\n\\)"        ; match comment line, if any
  1051.           "\\|"                           ; or
  1052.           "\\(^@ifinfo[ ]*\n\\)\\)?"      ; ifinfo line, if any
  1053.           (eval
  1054.            (cdr (assoc level texinfo-update-menu-higher-regexps))))
  1055.          nil
  1056.          'goto-beginning)
  1057.         (point))))))
  1058.  
  1059. (defun texinfo-update-menu-region-end (level)  
  1060.   "Locate end of higher level section this section is within.
  1061. Return position; do not move point.  Thus, if this level is a
  1062. subsection, find the node for the section this subsection is within.
  1063. If level is top or chapter, returns end of file.  Only argument is a
  1064. string of the general type of section."
  1065.   (let ((case-fold-search t))
  1066.     (save-excursion
  1067.       (if (re-search-forward
  1068.            (concat
  1069.             "\\(^@node\\).*\n"            ; match node line
  1070.             "\\(\\(\\(^@c\\).*\n\\)"      ; match comment line, if any
  1071.             "\\|"                         ; or
  1072.             "\\(^@ifinfo[ ]*\n\\)\\)?"    ; ifinfo line, if any
  1073.             (eval
  1074.              ;; Never finds end of level above chapter so goes to end.
  1075.              (cdr (assoc level texinfo-update-menu-higher-regexps))))
  1076.            nil
  1077.            'goto-end)
  1078.           (match-beginning 1)
  1079.         (point-max)))))
  1080.  
  1081. (defun texinfo-menu-first-node (beginning end)
  1082.   "Locate first node of the section the menu will be placed in.  
  1083. Return position; do not move point.
  1084. The menu will be located just before this position.  
  1085.  
  1086. First argument is the position of the beginning of the section in
  1087. which the menu will be located; second argument is the position of the
  1088. end of that region; it limits the search."
  1089.   
  1090.   (save-excursion
  1091.     (goto-char beginning)
  1092.     (forward-line 1)
  1093.     (re-search-forward "^@node" end t)
  1094.     (beginning-of-line)
  1095.     (point)))
  1096.  
  1097.  
  1098. ;;; Alists and regular expressions for defining hierarchical levels
  1099.  
  1100. (defvar texinfo-section-to-generic-alist
  1101.   '(("top" . "top")
  1102.  
  1103.     ("chapter" . "chapter")
  1104.     ("unnumbered" . "chapter")
  1105.     ("majorheading" . "chapter")
  1106.     ("chapheading" . "chapter")
  1107.     ("appendix" . "chapter")
  1108.     
  1109.     ("section" . "section")
  1110.     ("unnumberedsec" . "section")
  1111.     ("heading" . "section")
  1112.     ("appendixsec" . "section")
  1113.     
  1114.     ("subsection" . "subsection")
  1115.     ("unnumberedsubsec" . "subsection")
  1116.     ("subheading" . "subsection")
  1117.     ("appendixsubsec" . "subsection")
  1118.     
  1119.     ("subsubsection" . "subsubsection")
  1120.     ("unnumberedsubsubsec" . "subsubsection")
  1121.     ("subsubheading" . "subsubsection")
  1122.     ("appendixsubsubsec" . "subsubsection"))
  1123.   "*An alist of specific and corresponding generic Texinfo section types.
  1124. The keys are strings specifying specific types of section; the values
  1125. are strings of their corresponding general types.")
  1126.  
  1127. ;; We used to look for just sub, but that found @subtitle.
  1128. (defvar texinfo-section-types-regexp
  1129.   "^@\\(chapter \\|sect\\|subs\\|subh\\|unnum\\|major\\|chapheading \\|heading \\|appendix\\)"
  1130.   "Regexp matching chapter, section, other headings (but not the top node).")
  1131.  
  1132. (defvar texinfo-chapter-level-regexp 
  1133.   "chapter\\|unnumbered \\|appendix \\|majorheading\\|chapheading"
  1134.   "Regular expression matching just the Texinfo chapter level headings.")
  1135.  
  1136. (defvar texinfo-section-level-regexp 
  1137.   "section\\|unnumberedsec\\|heading \\|appendixsec"
  1138.   "Regular expression matching just the Texinfo section level headings.")
  1139.  
  1140. (defvar texinfo-subsection-level-regexp 
  1141.   "subsection\\|unnumberedsubsec\\|subheading\\|appendixsubsec"
  1142.   "Regular expression matching just the Texinfo subsection level headings.")
  1143.  
  1144. (defvar texinfo-subsubsection-level-regexp
  1145.   "subsubsection\\|unnumberedsubsubsec\\|subsubheading\\|appendixsubsubsec"
  1146.   "Regular expression matching just the Texinfo subsubsection level headings.")
  1147.  
  1148. (defvar texinfo-update-menu-same-level-regexps
  1149.   '(("top" . "top[ \t]+")
  1150.     ("chapter" . 
  1151.      (concat "\\(^@\\)\\(" texinfo-chapter-level-regexp "\\)[ \t]*"))
  1152.     ("section" . 
  1153.      (concat "\\(^@\\)\\(" texinfo-section-level-regexp "\\)[ \t]*"))
  1154.     ("subsection" .  
  1155.      (concat "\\(^@\\)\\(" texinfo-subsection-level-regexp "\\)[ \t]+"))
  1156.     ("subsubsection" . 
  1157.      (concat "\\(^@\\)\\(" texinfo-subsubsection-level-regexp "\\)[ \t]+")))
  1158.   "*Regexps for searching for same level sections in a Texinfo file.
  1159. The keys are strings specifying the general hierarchical level in the
  1160. document; the values are regular expressions.")
  1161.  
  1162. (defvar texinfo-update-menu-higher-regexps
  1163.   '(("top" . "^@node [ \t]*DIR") 
  1164.     ("chapter" . "^@node [ \t]*top[ \t]*\\(,\\|$\\)")
  1165.     ("section" .
  1166.      (concat 
  1167.       "\\(^@\\("
  1168.       texinfo-chapter-level-regexp
  1169.       "\\)[ \t]*\\)"))
  1170.     ("subsection" .
  1171.      (concat 
  1172.       "\\(^@\\("
  1173.       texinfo-section-level-regexp
  1174.       "\\|"
  1175.       texinfo-chapter-level-regexp
  1176.       "\\)[ \t]*\\)"))
  1177.     ("subsubsection" .
  1178.      (concat 
  1179.       "\\(^@\\("
  1180.       texinfo-subsection-level-regexp
  1181.       "\\|"
  1182.       texinfo-section-level-regexp
  1183.       "\\|"
  1184.       texinfo-chapter-level-regexp
  1185.       "\\)[ \t]*\\)")))
  1186.   "*Regexps for searching for higher level sections in a Texinfo file.
  1187. The keys are strings specifying the general hierarchical level in the
  1188. document; the values are regular expressions.")
  1189.  
  1190. (defvar texinfo-update-menu-lower-regexps
  1191.   '(("top" . 
  1192.      (concat 
  1193.       "\\(^@\\("
  1194.       texinfo-chapter-level-regexp
  1195.       "\\|"
  1196.       texinfo-section-level-regexp
  1197.       "\\|"
  1198.       texinfo-subsection-level-regexp
  1199.       "\\|"
  1200.       texinfo-subsubsection-level-regexp
  1201.       "\\)[ \t]*\\)"))
  1202.     ("chapter" . 
  1203.      (concat 
  1204.       "\\(^@\\("
  1205.       texinfo-section-level-regexp
  1206.       "\\|"
  1207.       texinfo-subsection-level-regexp
  1208.       "\\|"
  1209.       texinfo-subsubsection-level-regexp
  1210.       "\\)[ \t]*\\)"))
  1211.     ("section" .
  1212.      (concat 
  1213.       "\\(^@\\("
  1214.       texinfo-subsection-level-regexp
  1215.       "\\|"
  1216.       texinfo-subsubsection-level-regexp
  1217.       "\\)[ \t]+\\)"))
  1218.     ("subsection" .
  1219.      (concat 
  1220.       "\\(^@\\("
  1221.       texinfo-subsubsection-level-regexp
  1222.       "\\)[ \t]+\\)"))
  1223.     ("subsubsection" . "nothing lower"))
  1224.   "*Regexps for searching for lower level sections in a Texinfo file.
  1225. The keys are strings specifying the general hierarchical level in the
  1226. document; the values are regular expressions.")
  1227.  
  1228.  
  1229. ;;; Updating a node
  1230.  
  1231. ;;;###autoload
  1232. (defun texinfo-update-node (&optional region-p)
  1233.   "Without any prefix argument, update the node in which point is located.
  1234. Non-nil argument (prefix, if interactive) means update the nodes in the
  1235. marked region.
  1236.  
  1237. The functions for creating or updating nodes and menus, and their
  1238. keybindings, are:
  1239.  
  1240.     texinfo-update-node (&optional region-p)    \\[texinfo-update-node]
  1241.     texinfo-every-node-update ()                \\[texinfo-every-node-update]
  1242.     texinfo-sequential-node-update (&optional region-p)
  1243.  
  1244.     texinfo-make-menu (&optional region-p)      \\[texinfo-make-menu]
  1245.     texinfo-all-menus-update ()                 \\[texinfo-all-menus-update]
  1246.     texinfo-master-menu ()
  1247.  
  1248.     texinfo-indent-menu-description (column &optional region-p)
  1249.  
  1250. The `texinfo-column-for-description' variable specifies the column to
  1251. which menu descriptions are indented. Its default value is 32."
  1252.   
  1253.   (interactive "P")
  1254.   (if (not region-p)
  1255.       ;; update a single node
  1256.       (let ((auto-fill-function nil) (auto-fill-hook nil))
  1257.         (if (not (re-search-backward "^@node" (point-min) t))
  1258.             (error "Node line not found before this position."))
  1259.         (texinfo-update-the-node)
  1260.         (message "Done...updated the node.  You may save the buffer."))
  1261.     ;; else
  1262.     (let ((auto-fill-function nil)
  1263.       (auto-fill-hook nil)
  1264.           (beginning (region-beginning))
  1265.       (end (region-end)))
  1266.       (if (= end beginning)
  1267.           (error "Please mark a region!"))
  1268.       (save-restriction
  1269.     (narrow-to-region beginning end)
  1270.     (goto-char beginning)
  1271.         (push-mark (point) t)
  1272.     (while (re-search-forward "^@node" (point-max) t)
  1273.           (beginning-of-line)            
  1274.           (texinfo-update-the-node))
  1275.         (message "Done...updated nodes in region.  You may save the buffer.")))))
  1276.  
  1277. ;;;###autoload
  1278. (defun texinfo-every-node-update ()
  1279.   "Update every node in a Texinfo file."
  1280.   (interactive)
  1281.   (save-excursion
  1282.     (push-mark (point-max) t)
  1283.     (goto-char (point-min))
  1284.     ;; Using the mark to pass bounds this way
  1285.     ;; is kludgy, but it's not worth fixing. -- rms.
  1286.     (let ((mark-active t))
  1287.       (texinfo-update-node t))
  1288.     (message "Done...updated every node.       You may save the buffer.")))
  1289.  
  1290. (defun texinfo-update-the-node ()
  1291.   "Update one node.  Point must be at the beginning of node line.  
  1292. Leave point at the end of the node line."
  1293.   (texinfo-check-for-node-name)
  1294.   (texinfo-delete-existing-pointers)
  1295.   (message "Updating node: %s ... " (texinfo-copy-node-name))
  1296.   (save-restriction
  1297.     (widen)
  1298.     (let*
  1299.         ((case-fold-search t)
  1300.          (level (texinfo-hierarchic-level))
  1301.          (beginning (texinfo-update-menu-region-beginning level))
  1302.          (end (texinfo-update-menu-region-end level)))
  1303.       (if (string-equal level "top")
  1304.           (texinfo-top-pointer-case)
  1305.         ;; else
  1306.         (texinfo-insert-pointer beginning end level 'next)
  1307.         (texinfo-insert-pointer beginning end level 'previous)
  1308.         (texinfo-insert-pointer beginning end level 'up)
  1309.         (texinfo-clean-up-node-line)))))
  1310.  
  1311. (defun texinfo-top-pointer-case ()
  1312.   "Insert pointers in the Top node.  This is a special case.
  1313.  
  1314. The `Next' pointer is a pointer to a chapter or section at a lower
  1315. hierarchical level in the file.  The `Previous' and `Up' pointers are
  1316. to `(dir)'.  Point must be at the beginning of the node line, and is
  1317. left at the end of the node line."
  1318.  
  1319.   (texinfo-clean-up-node-line)
  1320.   (insert ", " 
  1321.           (save-excursion
  1322.             ;; There may be an @chapter or other such command between
  1323.             ;; the top node line and the next node line, as a title
  1324.             ;; for an `ifinfo' section. This @chapter command must
  1325.             ;; must be skipped.  So the procedure is to search for
  1326.             ;; the next `@node' line, and then copy its name.
  1327.             (if (re-search-forward "^@node" nil t)
  1328.                 (progn
  1329.                   (beginning-of-line)
  1330.                   (texinfo-copy-node-name))
  1331.               " "))
  1332.           ", (dir), (dir)"))
  1333.  
  1334. (defun texinfo-check-for-node-name ()
  1335.   "Determine whether the node has a node name.  Prompt for one if not.
  1336. Point must be at beginning of node line.  Does not move point."
  1337.   (save-excursion
  1338.     (let ((initial (texinfo-copy-next-section-title)))
  1339.       ;; This is not clean.  Use `interactive' to read the arg.
  1340.       (forward-word 1)                    ; skip over node command
  1341.       (skip-chars-forward " \t")          ; and over spaces
  1342.       (if (not (looking-at "[^,\t\n ]+")) ; regexp based on what Info looks for
  1343.                                           ; alternatively, use "[a-zA-Z]+"
  1344.         (let ((node-name
  1345.                (read-from-minibuffer
  1346.                 "Node name (use no @, commas, colons, or apostrophes): "
  1347.                 initial)))
  1348.           (insert " " node-name))))))
  1349.  
  1350. (defun texinfo-delete-existing-pointers ()
  1351.   "Delete `Next', `Previous', and `Up' pointers.  
  1352. Starts from the current position of the cursor, and searches forward
  1353. on the line for a comma and if one is found, deletes the rest of the
  1354. line, including the comma.  Leaves point at beginning of line."
  1355.   (let ((eol-point (save-excursion (end-of-line) (point))))
  1356.     (if (search-forward "," eol-point t)
  1357.         (delete-region (1- (point)) eol-point)))
  1358.   (beginning-of-line))
  1359.  
  1360. (defun texinfo-find-pointer (beginning end level direction)
  1361.   "Move point to section associated with next, previous, or up pointer.
  1362. Return type of pointer (either 'normal or 'no-pointer).
  1363.  
  1364. The first and second arguments bound the search for a pointer to the
  1365. beginning and end, respectively, of the enclosing higher level
  1366. section.  The third argument is a string specifying the general kind
  1367. of section such as \"chapter\" or \"section\".  When looking for the
  1368. `Next' pointer, the section found will be at the same hierarchical
  1369. level in the Texinfo file; when looking for the `Previous' pointer,
  1370. the section found will be at the same or higher hierarchical level in
  1371. the Texinfo file; when looking for the `Up' pointer, the section found
  1372. will be at some level higher in the Texinfo file.  The fourth argument
  1373. \(one of 'next, 'previous, or 'up\) specifies whether to find the
  1374. `Next', `Previous', or `Up' pointer."
  1375.   (let ((case-fold-search t))
  1376.     (cond ((eq direction 'next)
  1377.            (forward-line 3)             ; skip over current node
  1378.            ;; Search for section commands accompanied by node lines;
  1379.            ;; ignore section commands in the middle of nodes.
  1380.            (if (re-search-forward
  1381.                 ;; A `Top' node is never a next pointer, so won't find it.
  1382.                 (concat
  1383.                  ;; Match node line.
  1384.                  "\\(^@node\\).*\n"         
  1385.                  ;; Match comment or ifinfo line, if any
  1386.                  "\\(\\(\\(^@c\\).*\n\\)\\|\\(^@ifinfo[ ]*\n\\)\\)?" 
  1387.                  (eval
  1388.                   (cdr (assoc level texinfo-update-menu-same-level-regexps))))
  1389.                 end
  1390.                 t)
  1391.                'normal
  1392.              'no-pointer))
  1393.           ((eq direction 'previous)
  1394.            (if (re-search-backward
  1395.                 (concat
  1396.                  "\\("
  1397.                  ;; Match node line.
  1398.                  "\\(^@node\\).*\n"         
  1399.                  ;; Match comment or ifinfo line, if any
  1400.                  "\\(\\(\\(^@c\\).*\n\\)\\|\\(^@ifinfo[ ]*\n\\)\\)?" 
  1401.                  (eval
  1402.                   (cdr (assoc level texinfo-update-menu-same-level-regexps)))
  1403.                  "\\|"
  1404.                  ;; Match node line.
  1405.                  "\\(^@node\\).*\n"         
  1406.                  ;; Match comment or ifinfo line, if any
  1407.                  "\\(\\(\\(^@c\\).*\n\\)\\|\\(^@ifinfo[ ]*\n\\)\\)?" 
  1408.                  (eval
  1409.                   (cdr (assoc level texinfo-update-menu-higher-regexps)))
  1410.                  "\\|"
  1411.                  ;; Handle `Top' node specially.
  1412.                  "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
  1413.                  "\\)")
  1414.                 beginning
  1415.                 t)
  1416.                'normal
  1417.              'no-pointer))
  1418.           ((eq direction 'up)
  1419.            (if (re-search-backward
  1420.                 (concat
  1421.                  "\\("
  1422.                  ;; Match node line.
  1423.                  "\\(^@node\\).*\n"         
  1424.                  ;; Match comment or ifinfo line, if any
  1425.                  "\\(\\(\\(^@c\\).*\n\\)\\|\\(^@ifinfo[ ]*\n\\)\\)?" 
  1426.                  (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
  1427.                  "\\|"
  1428.                  ;; Handle `Top' node specially.
  1429.                  "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
  1430.                  "\\)")
  1431.                 (save-excursion
  1432.                   (goto-char beginning)
  1433.                   (beginning-of-line)
  1434.                   (point))
  1435.                 t)
  1436.                'normal
  1437.              'no-pointer))
  1438.           (t
  1439.            (error "texinfo-find-pointer: lack proper arguments")))))
  1440.  
  1441. (defun texinfo-pointer-name (kind)
  1442.   "Return the node name preceding the section command.
  1443. The argument is the kind of section, either normal or no-pointer."
  1444.   (let (name)
  1445.     (cond ((eq kind 'normal)
  1446.            (end-of-line)                ; this handles prev node top case
  1447.            (re-search-backward          ; when point is already 
  1448.             "^@node"                    ; at the beginning of @node line
  1449.             (save-excursion (forward-line -3))
  1450.             t)
  1451.            (setq name (texinfo-copy-node-name)))
  1452.       ((eq kind 'no-pointer)
  1453.        (setq name " ")))    ; put a blank in the pointer slot
  1454.     name))
  1455.  
  1456. (defun texinfo-insert-pointer (beginning end level direction)
  1457.   "Insert the `Next', `Previous' or `Up' node name at point.
  1458. Move point forward.  
  1459.  
  1460. The first and second arguments bound the search for a pointer to the
  1461. beginning and end, respectively, of the enclosing higher level
  1462. section.  The third argument is the hierarchical level of the Texinfo
  1463. file, a string such as \"section\".  The fourth argument is direction
  1464. towards which the pointer is directed, one of `next, `previous, or
  1465. 'up."
  1466.  
  1467.   (end-of-line)
  1468.   (insert
  1469.    ", "
  1470.    (save-excursion
  1471.      (texinfo-pointer-name
  1472.       (texinfo-find-pointer beginning end level direction)))))
  1473.  
  1474. (defun texinfo-clean-up-node-line ()
  1475.   "Remove extra commas, if any, at end of node line."
  1476.   (end-of-line)
  1477.   (skip-chars-backward ", ")
  1478.   (delete-region (point) (save-excursion (end-of-line) (point))))
  1479.  
  1480.  
  1481. ;;; Updating nodes sequentially
  1482. ; These sequential update functions insert `Next' or `Previous'
  1483. ; pointers that point to the following or preceding nodes even if they
  1484. ; are at higher or lower hierarchical levels.  This means that if a
  1485. ; section contains one or more subsections, the section's `Next'
  1486. ; pointer will point to the subsection and not the following section.
  1487. ; (The subsection to which `Next' points will most likely be the first
  1488. ; item on the section's menu.)
  1489.  
  1490. ;;;###autoload
  1491. (defun texinfo-sequential-node-update (&optional region-p)
  1492.   "Update one node (or many) in a Texinfo file with sequential pointers.
  1493.  
  1494. This function causes the `Next' or `Previous' pointer to point to the
  1495. immediately preceding or following node, even if it is at a higher or
  1496. lower hierarchical level in the document.  Continually pressing `n' or
  1497. `p' takes you straight through the file.
  1498.  
  1499. Without any prefix argument, update the node in which point is located.
  1500. Non-nil argument (prefix, if interactive) means update the nodes in the
  1501. marked region.
  1502.  
  1503. This command makes it awkward to navigate among sections and
  1504. subsections; it should be used only for those documents that are meant
  1505. to be read like a novel rather than a reference, and for which the
  1506. Info `g*' command is inadequate."
  1507.   
  1508.   (interactive "P")
  1509.   (if (not region-p)
  1510.       ;; update a single node
  1511.       (let ((auto-fill-function nil) (auto-fill-hook nil))
  1512.         (if (not (re-search-backward "^@node" (point-min) t))
  1513.             (error "Node line not found before this position."))
  1514.         (texinfo-sequentially-update-the-node)
  1515.         (message 
  1516.          "Done...sequentially updated the node .  You may save the buffer."))
  1517.     ;; else
  1518.     (let ((auto-fill-function nil)
  1519.       (auto-fill-hook nil)
  1520.           (beginning (region-beginning))
  1521.           (end (region-end)))
  1522.       (if (= end beginning)
  1523.           (error "Please mark a region!"))
  1524.       (save-restriction
  1525.         (narrow-to-region beginning end)
  1526.         (goto-char beginning)
  1527.         (push-mark (point) t)
  1528.         (while (re-search-forward "^@node" (point-max) t)
  1529.           (beginning-of-line)            
  1530.           (texinfo-sequentially-update-the-node))
  1531.         (message 
  1532.          "Done...updated the nodes in sequence.  You may save the buffer.")))))
  1533.  
  1534. (defun texinfo-sequentially-update-the-node ()
  1535.   "Update one node such that the pointers are sequential. 
  1536. A `Next' or `Previous' pointer points to any preceding or following node,
  1537. regardless of its hierarchical level."
  1538.  
  1539.         (texinfo-check-for-node-name)
  1540.         (texinfo-delete-existing-pointers)
  1541.         (message 
  1542.          "Sequentially updating node: %s ... " (texinfo-copy-node-name))
  1543.         (save-restriction
  1544.           (widen)
  1545.           (let*
  1546.               ((case-fold-search t)
  1547.                (level (texinfo-hierarchic-level)))
  1548.             (if (string-equal level "top")
  1549.                 (texinfo-top-pointer-case)
  1550.               ;; else
  1551.               (texinfo-sequentially-insert-pointer level 'next)
  1552.               (texinfo-sequentially-insert-pointer level 'previous)
  1553.               (texinfo-sequentially-insert-pointer level 'up)
  1554.               (texinfo-clean-up-node-line)))))
  1555.  
  1556. (defun texinfo-sequentially-find-pointer (level direction)
  1557.   "Find next or previous pointer sequentially in Texinfo file, or up pointer.
  1558. Move point to section associated with the pointer.  Find point even if
  1559. it is in a different section.
  1560.  
  1561. Return type of pointer (either 'normal or 'no-pointer).
  1562.  
  1563. The first argument is a string specifying the general kind of section
  1564. such as \"chapter\" or \"section\".  The section found will be at the
  1565. same hierarchical level in the Texinfo file, or, in the case of the up
  1566. pointer, some level higher.  The second argument (one of 'next,
  1567. 'previous, or 'up) specifies whether to find the `Next', `Previous',
  1568. or `Up' pointer."
  1569.   (let ((case-fold-search t))  
  1570.     (cond ((eq direction 'next)
  1571.            (forward-line 3)             ; skip over current node
  1572.            (if (re-search-forward 
  1573.                 texinfo-section-types-regexp
  1574.                 (point-max)
  1575.                 t)
  1576.                'normal
  1577.              'no-pointer))
  1578.           ((eq direction 'previous)
  1579.            (if (re-search-backward 
  1580.                 texinfo-section-types-regexp
  1581.                 (point-min)
  1582.                 t)
  1583.                'normal
  1584.              'no-pointer))
  1585.           ((eq direction 'up)
  1586.            (if (re-search-backward
  1587.                 (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
  1588.                 beginning
  1589.                 t)
  1590.                'normal
  1591.              'no-pointer))
  1592.           (t
  1593.            (error "texinfo-sequential-find-pointer: lack proper arguments")))))
  1594.  
  1595. (defun texinfo-sequentially-insert-pointer (level direction)
  1596.   "Insert the `Next', `Previous' or `Up' node name at point.
  1597. Move point forward.  
  1598.  
  1599. The first argument is the hierarchical level of the Texinfo file, a
  1600. string such as \"section\".  The second argument is direction, one of
  1601. `next, `previous, or 'up."
  1602.  
  1603.   (end-of-line)
  1604.   (insert
  1605.    ", "
  1606.    (save-excursion
  1607.      (texinfo-pointer-name
  1608.       (texinfo-sequentially-find-pointer level direction)))))
  1609.  
  1610.  
  1611. ;;; Inserting `@node' lines
  1612. ; The `texinfo-insert-node-lines' function inserts `@node' lines as needed
  1613. ; before the `@chapter', `@section', and such like lines of a region
  1614. ; in a Texinfo file.
  1615.  
  1616. (defun texinfo-insert-node-lines (beginning end &optional title-p)
  1617.   "Insert missing `@node' lines in region of Texinfo file.
  1618. Non-nil argument (prefix, if interactive) means also to insert the
  1619. section titles as node names; and also to insert the section titles as
  1620. node names in pre-existing @node lines that lack names."
  1621.   (interactive "r\nP")
  1622.  
  1623.   ;; Use marker; after inserting node lines, leave point at end of
  1624.   ;; region and mark at beginning.
  1625.  
  1626.   (let (beginning-marker end-marker title last-section-position)
  1627.  
  1628.     ;; Save current position on mark ring and set mark to end.
  1629.     (push-mark end t)                   
  1630.     (setq end-marker (mark-marker))        
  1631.  
  1632.     (goto-char beginning)
  1633.     (while (re-search-forward
  1634.             texinfo-section-types-regexp 
  1635.             end-marker
  1636.             'end)
  1637.       ;; Copy title if desired.
  1638.       (if title-p
  1639.           (progn 
  1640.             (beginning-of-line)
  1641.             (forward-word 1)
  1642.             (skip-chars-forward " \t")
  1643.             (setq title (buffer-substring
  1644.                          (point)
  1645.                          (save-excursion (end-of-line) (point))))))
  1646.       ;; Insert node line if necessary.
  1647.       (if (re-search-backward
  1648.            "^@node" 
  1649.            ;; Avoid finding previous node line if node lines are close.
  1650.            (or last-section-position    
  1651.                (save-excursion (forward-line -2) (point))) t)
  1652.           ;;  @node is present, and point at beginning of that line
  1653.           (forward-word 1)          ; Leave point just after @node.
  1654.         ;; Else @node missing; insert one.
  1655.         (beginning-of-line)         ; Beginning of `@section' line.
  1656.         (insert "@node\n")
  1657.         (backward-char 1))          ; Leave point just after `@node'.
  1658.       ;; Insert title if desired.
  1659.       (if title-p
  1660.           (progn
  1661.             (skip-chars-forward " \t")
  1662.             ;; Use regexp based on what info looks for
  1663.             ;; (alternatively, use "[a-zA-Z]+");
  1664.             ;; this means we only insert a title if none exists.
  1665.             (if (not (looking-at "[^,\t\n ]+")) 
  1666.                 (progn
  1667.                   (beginning-of-line) 
  1668.                   (forward-word 1)
  1669.                   (insert " " title)
  1670.                   (message "Inserted title %s ... " title)))))
  1671.       ;; Go forward beyond current section title.
  1672.       (re-search-forward texinfo-section-types-regexp 
  1673.                          (save-excursion (forward-line 3) (point)) t)
  1674.       (setq last-section-position (point))
  1675.       (forward-line 1))
  1676.  
  1677.     ;; Leave point at end of region, mark at beginning.
  1678.     (set-mark beginning)
  1679.  
  1680.     (if title-p
  1681.       (message
  1682.        "Done inserting node lines and titles.  You may save the buffer.")
  1683.     (message "Done inserting node lines.  You may save the buffer."))))
  1684.  
  1685.  
  1686. ;;; Update and create menus for multi-file Texinfo sources
  1687.  
  1688. ;;  1. M-x texinfo-multiple-files-update 
  1689. ;;
  1690. ;;     Read the include file list of an outer Texinfo file and
  1691. ;;     update all highest level nodes in the files listed and insert a
  1692. ;;     main menu in the outer file after its top node.
  1693.  
  1694. ;;  2. C-u M-x texinfo-multiple-files-update 
  1695. ;;
  1696. ;;     Same as 1, but insert a master menu.  (Saves reupdating lower
  1697. ;;     level menus and nodes.)  This command simply reads every menu,
  1698. ;;     so if the menus are wrong, the master menu will be wrong.
  1699. ;;     Similarly, if the lower level node pointers are wrong, they
  1700. ;;     will stay wrong.
  1701.  
  1702. ;;  3. C-u 2 M-x texinfo-multiple-files-update 
  1703. ;;
  1704. ;;     Read the include file list of an outer Texinfo file and
  1705. ;;     update all nodes and menus in the files listed and insert a
  1706. ;;     master menu in the outer file after its top node.
  1707.  
  1708. ;;; Note: these functions:
  1709. ;;;
  1710. ;;;   * Do not save or delete any buffers.  You may fill up your memory.
  1711. ;;;   * Do not handle any pre-existing nodes in outer file.  
  1712. ;;;     Hence, you may need a file for indices.
  1713.  
  1714.  
  1715. ;;; Auxiliary functions for multiple file updating
  1716.  
  1717. (defun texinfo-multi-file-included-list (outer-file)
  1718.   "Return a list of the included files in OUTER-FILE."
  1719.   (let ((included-file-list (list outer-file))
  1720.         start)
  1721.     (save-excursion
  1722.       (switch-to-buffer (find-file-noselect outer-file))
  1723.       (widen)
  1724.       (goto-char (point-min))
  1725.       (while (re-search-forward "^@include" nil t)
  1726.         (skip-chars-forward " \t")
  1727.         (setq start (point))
  1728.         (end-of-line)
  1729.         (skip-chars-backward " \t")   
  1730.         (setq included-file-list
  1731.               (cons (buffer-substring start (point))
  1732.                     included-file-list)))
  1733.       (nreverse included-file-list))))
  1734.  
  1735. (defun texinfo-copy-next-section-title ()
  1736.   "Return the name of the immediately following section as a string.
  1737.  
  1738. Start with point at the beginning of the node line.  Leave point at the
  1739. same place.  If there is no title, returns an empty string."
  1740.  
  1741.   (save-excursion
  1742.     (end-of-line)
  1743.     (let ((node-end (or 
  1744.                         (save-excursion
  1745.                           (if (re-search-forward "\\(^@node\\)" nil t)
  1746.                               (match-beginning 0)))
  1747.                         (point-max))))
  1748.       (if (re-search-forward texinfo-section-types-regexp node-end t)
  1749.           (progn
  1750.             (beginning-of-line)
  1751.             ;; copy title
  1752.             (let ((title
  1753.                    (buffer-substring
  1754.                     (progn (forward-word 1)           ; skip over section type
  1755.                            (skip-chars-forward " \t") ; and over spaces
  1756.                            (point))
  1757.                     (progn (end-of-line) (point)))))
  1758.               title))
  1759.         ""))))
  1760.  
  1761. (defun texinfo-multi-file-update (files &optional update-everything)
  1762.   "Update first node pointers in each file in FILES.
  1763. Return a list of the node names.
  1764.  
  1765. The first file in the list is an outer file; the remaining are
  1766. files included in the outer file with `@include' commands.
  1767.  
  1768. If optional arg UPDATE-EVERYTHING non-nil, update every menu and
  1769. pointer in each of the included files.
  1770.  
  1771. Also update the `Top' level node pointers of the outer file.
  1772.  
  1773. Requirements:
  1774.  
  1775.   * the first file in the FILES list must be the outer file,
  1776.   * each of the included files must contain exactly one highest
  1777.     hierarchical level node, 
  1778.   * this node must be the first node in the included file,
  1779.   * each highest hierarchical level node must be of the same type.
  1780.  
  1781. Thus, normally, each included file contains one, and only one,
  1782. chapter."
  1783.  
  1784. ; The menu-list has the form:
  1785. ;     \(\(\"node-name1\" . \"title1\"\) 
  1786. ;       \(\"node-name2\" . \"title2\"\) ... \)
  1787. ; However, there does not need to be a title field and this function
  1788. ; does not fill it; however a comment tells you how to do so.
  1789. ; You would use the title field if you wanted to insert titles in the
  1790. ; description slot of a menu as a description.
  1791.   
  1792.   (let ((case-fold-search t)
  1793.         menu-list)
  1794.     
  1795.     ;; Find the name of the first node of the first included file.
  1796.     (switch-to-buffer (find-file-noselect (car (cdr files))))
  1797.     (widen)
  1798.     (goto-char (point-min))
  1799.     (if (not (re-search-forward "^@node" nil t))
  1800.         (error "No `@node' line found in %s !" (buffer-name)))
  1801.     (beginning-of-line)
  1802.     (texinfo-check-for-node-name)
  1803.     (setq next-node-name (texinfo-copy-node-name))
  1804.     
  1805.     (setq menu-list
  1806.           (cons (cons 
  1807.                  next-node-name
  1808.                  (prog1 "" (forward-line 1)))
  1809.                 ;; Use following to insert section titles automatically.
  1810.                 ;; (texinfo-copy-next-section-title)
  1811.                 menu-list))
  1812.  
  1813.     ;; Go to outer file
  1814.     (switch-to-buffer (find-file-noselect (car files)))
  1815.     (goto-char (point-min))
  1816.     (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
  1817.         (error "This buffer needs a Top node!"))
  1818.     (beginning-of-line)
  1819.     (texinfo-delete-existing-pointers)
  1820.     (end-of-line)
  1821.     (insert ", " next-node-name ", (dir), (dir)")
  1822.     (beginning-of-line)
  1823.     (setq previous-node-name "Top")
  1824.     (setq files (cdr files))
  1825.     
  1826.     (while files
  1827.       
  1828.       (if (not (cdr files))
  1829.           ;; No next file
  1830.           (setq next-node-name "")
  1831.         ;; Else,
  1832.         ;; find the name of the first node in the next file.
  1833.         (switch-to-buffer (find-file-noselect (car (cdr files))))
  1834.         (widen)
  1835.         (goto-char (point-min))
  1836.         (if (not (re-search-forward "^@node" nil t))
  1837.             (error "No `@node' line found in %s !" (buffer-name)))
  1838.         (beginning-of-line)
  1839.         (texinfo-check-for-node-name)
  1840.         (setq next-node-name (texinfo-copy-node-name))
  1841.         (setq menu-list
  1842.               (cons (cons 
  1843.                      next-node-name
  1844.                      (prog1 "" (forward-line 1)))
  1845.                     ;; Use following to insert section titles automatically.
  1846.                     ;; (texinfo-copy-next-section-title)
  1847.                     menu-list)))
  1848.  
  1849.       ;; Go to node to be updated.
  1850.       (switch-to-buffer (find-file-noselect (car files)))
  1851.       (goto-char (point-min))
  1852.       (if (not (re-search-forward "^@node" nil t))
  1853.           (error "No `@node' line found in %s !" (buffer-name)))
  1854.       (beginning-of-line)
  1855.       
  1856.       ;; Update other menus and nodes if requested.
  1857.       (if update-everything (texinfo-all-menus-update t))
  1858.  
  1859.       (beginning-of-line)
  1860.       (texinfo-delete-existing-pointers)
  1861.       (end-of-line)
  1862.       (insert ", " next-node-name ", " previous-node-name ", " up-node-name)
  1863.       
  1864.       (beginning-of-line)
  1865.       (setq previous-node-name (texinfo-copy-node-name))
  1866.       
  1867.       (setq files (cdr files)))
  1868.     (nreverse menu-list)))
  1869.  
  1870. (defun texinfo-multi-files-insert-main-menu (menu-list)
  1871.   "Insert formatted main menu at point.
  1872. Indents the first line of the description, if any, to the value of
  1873. texinfo-column-for-description."
  1874.  
  1875.   (insert "@menu\n")
  1876.   (while menu-list
  1877.     ;; Every menu entry starts with a star and a space.
  1878.     (insert "* ")
  1879.     
  1880.     ;; Insert the node name (and menu entry name, if present).
  1881.     (let ((node-part (car (car menu-list))))
  1882.       (if (stringp node-part)
  1883.           ;; "Double colon" entry line; menu entry and node name are the same,
  1884.           (insert (format "%s::" node-part))  
  1885.         ;; "Single colon" entry line; menu entry and node name are different.
  1886.         (insert (format "%s: %s." (car node-part) (cdr node-part)))))
  1887.     
  1888.     ;; Insert the description, if present.
  1889.     (if (cdr (car menu-list))
  1890.         (progn
  1891.           ;; Move to right place.
  1892.           (indent-to texinfo-column-for-description 2) 
  1893.           ;; Insert description.
  1894.           (insert (format "%s" (cdr (car menu-list))))))  
  1895.  
  1896.     (insert "\n") ; end this menu entry
  1897.     (setq menu-list (cdr menu-list)))
  1898.   (insert "@end menu"))
  1899.  
  1900. (defun texinfo-multi-file-master-menu-list (files-list)
  1901.   "Return master menu list from files in FILES-LIST.
  1902. Menu entries in each file collected using `texinfo-master-menu-list'.
  1903.  
  1904. The first file in FILES-LIST must be the outer file; the others must
  1905. be the files included within it.  A main menu must already exist."
  1906.   (save-excursion
  1907.     (let (master-menu-list)
  1908.       (while files-list
  1909.         (switch-to-buffer (find-file-noselect (car files-list)))
  1910.         (message "Working on: %s " (current-buffer))
  1911.         (goto-char (point-min))
  1912.         (setq master-menu-list
  1913.               (append master-menu-list (texinfo-master-menu-list)))
  1914.         (setq files-list (cdr files-list)))
  1915.       master-menu-list)))
  1916.  
  1917.  
  1918. ;;; The multiple-file update function
  1919.  
  1920. (defun texinfo-multiple-files-update
  1921.   (outer-file &optional update-everything make-master-menu)
  1922.   "Update first node pointers in each file included in OUTER-FILE;
  1923. create or update the `Top' level node pointers and the main menu in
  1924. the outer file that refers to such nodes.  This does not create or
  1925. update menus or pointers within the included files.
  1926.  
  1927. With optional MAKE-MASTER-MENU argument (prefix arg, if interactive),
  1928. insert a master menu in OUTER-FILE in addition to creating or updating
  1929. pointers in the first @node line in each included file and creating or
  1930. updating the `Top' level node pointers of the outer file.  This does
  1931. not create or update other menus and pointers within the included
  1932. files.
  1933.  
  1934. With optional UPDATE-EVERYTHING argument (numeric prefix arg, if
  1935. interactive), update all the menus and all the `Next', `Previous', and
  1936. `Up' pointers of all the files included in OUTER-FILE before inserting
  1937. a master menu in OUTER-FILE.  Also, update the `Top' level node
  1938. pointers of OUTER-FILE.
  1939.  
  1940. Notes: 
  1941.  
  1942.   * this command does NOT save any files--you must save the
  1943.     outer file and any modified, included files.
  1944.  
  1945.   * except for the `Top' node, this command does NOT handle any
  1946.     pre-existing nodes in the outer file; hence, indices must be
  1947.     enclosed in an included file.
  1948.  
  1949. Requirements:
  1950.  
  1951.   * each of the included files must contain exactly one highest
  1952.     hierarchical level node, 
  1953.   * this highest node must be the first node in the included file,
  1954.   * each highest hierarchical level node must be of the same type.
  1955.  
  1956. Thus, normally, each included file contains one, and only one,
  1957. chapter."
  1958.   
  1959.   (interactive (cons
  1960.                 (read-string
  1961.                  "Name of outer `include' file: "
  1962.                  (buffer-file-name))
  1963.                 (cond ((not current-prefix-arg)
  1964.                        '(nil nil))
  1965.                       ((listp current-prefix-arg)
  1966.                        '(t nil))   ; make-master-menu 
  1967.                       ((numberp current-prefix-arg)
  1968.                        '(t t))     ; update-everything
  1969.                       )))
  1970.  
  1971.   (let* ((included-file-list (texinfo-multi-file-included-list outer-file))
  1972.          (files included-file-list)
  1973.          main-menu-list
  1974.          next-node-name
  1975.          previous-node-name
  1976.          (up-node-name "Top"))
  1977.  
  1978. ;;; Update the pointers 
  1979. ;;; and collect the names of the nodes and titles
  1980.     (setq main-menu-list (texinfo-multi-file-update files update-everything))
  1981.  
  1982. ;;; Insert main menu
  1983.  
  1984.   ;; Go to outer file
  1985.   (switch-to-buffer (find-file-noselect (car included-file-list)))
  1986.   (if (texinfo-old-menu-p
  1987.        (point-min)
  1988.        (save-excursion
  1989.          (re-search-forward "^@include")
  1990.          (beginning-of-line)
  1991.          (point)))
  1992.  
  1993.       ;; If found, leave point after word `menu' on the `@menu' line.
  1994.       (progn
  1995.         (texinfo-incorporate-descriptions main-menu-list)
  1996.         ;; Delete existing menu.
  1997.         (beginning-of-line)
  1998.         (delete-region
  1999.          (point)
  2000.          (save-excursion (re-search-forward "^@end menu") (point)))
  2001.         ;; Insert main menu
  2002.         (texinfo-multi-files-insert-main-menu main-menu-list))
  2003.  
  2004.     ;; Else no current menu; insert it before `@include'
  2005.     (texinfo-multi-files-insert-main-menu main-menu-list))
  2006.  
  2007. ;;; Insert master menu
  2008.  
  2009.   (if make-master-menu
  2010.       (progn
  2011.         ;; First, removing detailed part of any pre-existing master menu
  2012.         (goto-char (point-min))
  2013.         (if (re-search-forward texinfo-master-menu-header nil t)
  2014.             ;; Remove detailed master menu listing
  2015.             (progn
  2016.               (goto-char (match-beginning 0))
  2017.               (let ((end-of-detailed-menu-descriptions
  2018.                      (save-excursion     ; beginning of end menu line
  2019.                        (goto-char (texinfo-menu-end))
  2020.                        (beginning-of-line) (forward-char -1)
  2021.                        (point))))
  2022.                 (delete-region (point) end-of-detailed-menu-descriptions))))
  2023.  
  2024.         ;; Create a master menu and insert it
  2025.         (texinfo-insert-master-menu-list 
  2026.          (texinfo-multi-file-master-menu-list
  2027.           included-file-list)))))
  2028.  
  2029.   ;; Remove unwanted extra lines.
  2030.   (save-excursion
  2031.     (goto-char (point-min))
  2032.       
  2033.     (re-search-forward "^@menu")
  2034.     (forward-line -1)
  2035.     (insert  "\n") ; Ensure at least one blank line.
  2036.     (delete-blank-lines)
  2037.       
  2038.     (re-search-forward "^@end menu")
  2039.     (forward-line 1)
  2040.     (insert  "\n") ; Ensure at least one blank line.
  2041.     (delete-blank-lines))
  2042.  
  2043.   (message "Multiple files updated."))
  2044.  
  2045.  
  2046. ;;; Place `provide' at end of file.
  2047. (provide 'texnfo-upd)
  2048.  
  2049. ;;; texnfo-upd.el ends here
  2050.